package rexsee.device;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

public class RexseeBattery implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Battery";

	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	public static final String EVENT_ONBATTERYCHANGED = "onBatteryChanged";

	private final Browser mBrowser;
	private String mBatteryStatus = null;

	public RexseeBattery(Browser browser) {
		mBrowser = browser;
		mBrowser.eventList.add(EVENT_ONBATTERYCHANGED);
	}

	private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
				int status = intent.getIntExtra("status", 0);
				int health = intent.getIntExtra("health", 0);
				boolean present = intent.getBooleanExtra("present", false);
				int level = intent.getIntExtra("level", 0);
				int scale = intent.getIntExtra("scale", 0);
				int icon_small = intent.getIntExtra("icon-small", 0);
				int plugged = intent.getIntExtra("plugged", 0);
				int voltage = intent.getIntExtra("voltage", 0);
				int temperature = intent.getIntExtra("temperature", 0);
				String technology = intent.getStringExtra("technology");

				String statusString = "";
				switch (status) {
					case BatteryManager.BATTERY_STATUS_UNKNOWN :
						statusString = "unknown";
						break;
					case BatteryManager.BATTERY_STATUS_CHARGING :
						statusString = "charging";
						break;
					case BatteryManager.BATTERY_STATUS_DISCHARGING :
						statusString = "discharging";
						break;
					case BatteryManager.BATTERY_STATUS_NOT_CHARGING :
						statusString = "not charging";
						break;
					case BatteryManager.BATTERY_STATUS_FULL :
						statusString = "full";
						break;
				}

				String healthString = "";
				switch (health) {
					case BatteryManager.BATTERY_HEALTH_UNKNOWN :
						healthString = "unknown";
						break;
					case BatteryManager.BATTERY_HEALTH_GOOD :
						healthString = "good";
						break;
					case BatteryManager.BATTERY_HEALTH_OVERHEAT :
						healthString = "overheat";
						break;
					case BatteryManager.BATTERY_HEALTH_DEAD :
						healthString = "dead";
						break;
					case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE :
							healthString = "voltage";
							break;
						case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE :
						healthString = "unspecified failure";
						break;
				}

				String acString = "";
				switch (plugged) {
					case BatteryManager.BATTERY_PLUGGED_AC :
						acString = "plugged ac";
						break;
					case BatteryManager.BATTERY_PLUGGED_USB :
						acString = "plugged usb";
						break;
				}

				mBatteryStatus = "{";
				mBatteryStatus += "\"status\":\"" + statusString + "\"";
				mBatteryStatus += ",\"health\":\"" + healthString + "\"";
				mBatteryStatus += ",\"plugged\":\"" + acString + "\"";
				mBatteryStatus += ",\"present\":" + String.valueOf(present);
				mBatteryStatus += ",\"level\":" + level;
				mBatteryStatus += ",\"scale\":" + scale;
				mBatteryStatus += ",\"icon_small\":" + icon_small;
				mBatteryStatus += ",\"voltage\":" + voltage;
				mBatteryStatus += ",\"temperature\":" + temperature;
				mBatteryStatus += ",\"technology\":\"" + technology + "\"";
				mBatteryStatus += "}";

				mBrowser.eventList.run(EVENT_ONBATTERYCHANGED);

			}
		}
	};

	//JavaScript Interface
	public boolean enable() {
		try {
			IntentFilter filter = new IntentFilter();
			filter.addAction(Intent.ACTION_BATTERY_CHANGED);
			mBrowser.getContext().registerReceiver(mBroadcastReceiver, filter);
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public boolean disable() {
		try {
			mBrowser.getContext().unregisterReceiver(mBroadcastReceiver);
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public String getLastKnownInfo() {
		return (mBatteryStatus == null) ? "{}" : mBatteryStatus;
	}

}
